app.controller("shopcar", ["$scope", "$rootScope", "$state", "$http", function($scope, $rootScope, $state, $http) {
	$rootScope.page.showfooter = true;
	$scope.page.rongcuo = false;
	$rootScope.page.footer = 'shopcar';
	$rootScope.record()
	$rootScope.findshopcarNum()	
	if(!localStorage.getItem("user_ID")){
		$rootScope.register()
		$scope.page.rongcuo = true;
	}else{
		$http({
		method: 'post',
		url: '' + $rootScope.Location + 'Order/cart_list',
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded'
		},
		data:{
			user_Id:localStorage.getItem("user_ID"),
			token:localStorage.getItem("token")
		}
	}).success(function(data) {       
		console.log(data)		
		if(!data["list"]){
			$scope.page.rongcuo = true;
			$(".Settlement").hide()
		}else{
			$(".Settlement").show()
			$scope.shopcarList = data["list"]
		}
	})
	}
	setTimeout(function(){
		//单选
	$(".shopcar_list .shopCarlist .icon").click(function(){
		if($(this).attr("class") == "iconfont icon icon-kongxinweigou") {
			$(this).removeClass("icon-kongxinweigou").addClass("icon-shixinduigoudaibiankuang");
			$(this).parent().parent().siblings().find("i").removeClass("icon-shixinduigoudaibiankuang").addClass("icon-kongxinweigou");
		}else{
			$(this).removeClass("icon-shixinduigoudaibiankuang").addClass("icon-kongxinweigou");
			$(".allcheck").removeClass("icon-shixinduigoudaibiankuang").addClass("icon-kongxinweigou");
		}
//		if($(".icon-shixinduigoudaibiankuang").length ==  $(".shopCarlist").length) { //单选全选时 全选选中；
//			$(".allcheck").removeClass("icon-kongxinweigou").addClass("icon-shixinduigoudaibiankuang");
//		}
		allPrice();
	})
	//滑动删除
	for(var i = 0; i < $(".shopCarMove").length; i++) {
			$(".shopCarMove").eq(i).attr("dataIndex", i);
		}
		shopCarRemove('.shopCarMove');
	//全选
//	$(".Settlement .iconfont").click(function(){
//
//		if($(this).attr("class") == "iconfont allcheck icon-kongxinweigou"){
//			$(this).removeClass("icon-kongxinweigou").addClass("icon-shixinduigoudaibiankuang");
//			$(".icon-kongxinweigou").attr("class", "iconfont icon icon-shixinduigoudaibiankuang");
//		}else{
//			$(this).removeClass("icon-shixinduigoudaibiankuang").addClass("icon-kongxinweigou");
//			$(".icon-shixinduigoudaibiankuang").attr("class", "iconfont icon icon-kongxinweigou");
//		}
//		
//		allPrice()
//	})
	//加法
	$(".number .add").click(function(){
		var addsku = $(this).parent().attr("sku")
		console.log(addsku)
		var addHtml = $(this).parent().find(".numInput").html()*1
		addHtml++
		$http({
		method: 'post',
		url: '' + $rootScope.Location + 'Order/upcart',
		headers: {
			'Content-Type': 'application/x-www-form-urlencoded'
		},
		data:{
			user_Id:$rootScope.userID,
			token:$rootScope.token,
			cart_Id:addsku,
			type:2
		},
	}).success(function(data) {
		console.log(data)
	})
		$(this).parent().find(".numInput").html(addHtml)
		allPrice()
	})
	//减法
	$(".number .reduce").click(function(){
		var reducesku = $(this).parent().attr("sku")
		var reduceHtml = $(this).parent().find(".numInput").html()*1	
		if(reduceHtml<=1){
			reduceHtml=1
		}else{
			reduceHtml--
			$http({
			method: 'post',
			url: '' + $rootScope.Location + 'Order/upcart',
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded'
			},
			data:{
				user_Id:$rootScope.userID,
				token:$rootScope.token,
				cart_Id:reducesku,
				type:1
			},
		}).success(function(data) {
			console.log(data)
		})
		}
		$(this).parent().find(".numInput").html(reduceHtml)
		allPrice()
	})
	//计算总价
	function allPrice() { //计算总价
		var shopCarAllPrice = 0;
		for(var i = 0; i < $(".shopCarlist").length; i++) {
			var shopCarlistClass = $(".shopCarlist").eq(i).find(".iconfont").attr("class");
			if(shopCarlistClass == "iconfont icon icon-shixinduigoudaibiankuang") {
				var shopCarlistNum = $(".numInput").eq(i).html(); //商品数量
//				console.log(shopCarlistNum);
				var shopCarlistPrice = $(".p4").eq(i).html().substring(1)*1; //商品价格
				var oneShopCarlistPrice = shopCarlistNum * shopCarlistPrice; //一种商品的价格；
				shopCarAllPrice += oneShopCarlistPrice;
			}
		}
		shopCarAllPrice = shopCarAllPrice.toFixed(2);
		//		console.log(shopCarAllPrice);
		$(".allprice").html("¥" + shopCarAllPrice);
	}
	//滑动删除
		function shopCarRemove(box) {
		var boxList = $(box);
		var boxHeight = boxList.outerHeight();
		var btnList = null;
		var btnWidth = null;
		boxList.on("touchstart", function(e) {
			btnList = $('.shopCarDelete');
			btnWidth = -btnList.outerWidth();
			this.startX = e.originalEvent.targetTouches[0].clientX;
			this.startY = e.originalEvent.targetTouches[0].clientY;
		});
		boxList.on("touchend", function(e) {
			var dataIndex = $(this).attr("dataIndex");
			this.MissX = this.startX - e.originalEvent.changedTouches[0].clientX;
			this.MissY = this.startY - e.originalEvent.changedTouches[0].clientY;
			if(Math.abs(this.MissY) < boxHeight && this.MissX > 25) {
				btnList.eq(dataIndex).stop(true, true).animate({
					"right": btnWidth
				}, 80);
				$(this).stop(true, true).animate({
					"right": -btnWidth
				}, 80);
				//				$(this).parent().siblings().find(".shopCarMove").stop(true,true).animate({"right":0},250);
				//				$(this).parent().siblings().find(".shopCarDelete").stop(true,true).animate({"right":btnWidth},250);
			} else if(Math.abs(this.MissY) < boxHeight && this.MissX < -25) {
				btnList.eq(dataIndex).stop(true, true).animate({
					"right": btnWidth
				}, 80);
				$(this).stop(true, true).animate({
					"right": "0px"
				}, 80);
			}
		});
		//点击删除
		$(".shopCarDelete").click(function() {
			var shopCarlistLength = $(this).parents(".shopcar_list").find(".shopCarlist").length; //每个商铺商品的长度
			if(shopCarlistLength > 1) {
				$(this).parent().parent().remove();
			} else {
				$(this).parents(".shopcar_list").remove();				
			}
			if($(".shopCarlist").length == 0) {
				$scope.page.rongcuo = true;
				$(".Settlement").hide()
			}
			var delsku = $(this).attr("sku")
			
			
				$http({
				method: 'post',
				url: '' + $rootScope.Location + 'Order/delcart',
				headers: {
					'Content-Type': 'application/x-www-form-urlencoded'
				},
				data:{
					user_Id:localStorage.getItem("user_ID"),
					token:localStorage.getItem("token"),
					cart_Id:","+delsku
				},
				}).success(function(data) {
					console.log(data)
					allPrice()
				})
//			if($(".shopCarlist").length == 0) {
//				$scope.page.rongcuo = true;  //容错展示
//			}
//			var goodsSku = $(this).parent().parent().attr("sku");
//			$http.jsonp("http://" + $rootScope.Location + "/cart/deleteCart?callback=JSON_CALLBACK&skuCode=" + goodsSku + "").success(function(data) {
				allPrice();
//			})
		})
	}	
},500)
	var channel = localStorage.getItem("channel")
	$scope.toPay = function(){
		var str = ""
		for(var i = 0; i < $(".shopCarlist").length; i++) {
			var shopCarlistClass = $(".shopCarlist").eq(i).find(".iconfont").attr("class");
			if(shopCarlistClass == "iconfont icon icon-shixinduigoudaibiankuang") {
				str += ","+$(".shopCarlist").eq(i).attr("sku")
			}
		}
		if(!str){
			$(".addressdialog").show();
			$(".addressdialog").html("请选择你需要购买的商品");
			$(".addressdialog").fadeOut(1200);
		}else{
			$state.go("main.payment",{
					str:str,num:1
				})
			
		}		
	}
}])